package com.groupbuying.easyexcel;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/* Excel生成工具 */
public class ExcelUtil {

    /* 生成Excel文件，存入OSS中，并返回文件相关信息 */
    public static Map<String, String> generateExcel(List<Map<String, Object>> preList, String fileName, List<String> title) throws IOException {

        /* 使用2007+的扩展名 .xlsx，提升扩展性 */
        String fullName = fileName + ".xlsx";

        /* 构建相应的Excel文件 */
        OutputStream out = new FileOutputStream(fullName);
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        Sheet sheet1 = new Sheet(1, 0);
        sheet1.setSheetName("sheet1");
        List<List<String>> data = new ArrayList<>();
        for (Map<String, Object> map : preList) {
            List<String> item = new ArrayList();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String mapValue = null;
                Object obj = entry.getValue();

                if (obj instanceof BigDecimal) {
                    obj = (BigDecimal) obj;
                    mapValue = obj.toString();
                } else {
                    mapValue = (String) entry.getValue();
                }
                item.add(mapValue);
            }
            data.add(item);
        }

        /* 设置标题 */
        Table table = new Table(1);
        List<List<String>> head = new ArrayList<>();
        for(String temp : title){
            head.add(Arrays.asList(temp));
        }
        table.setHead(head);

        /* 写入Excel */
        writer.write0(data, sheet1, table);
        writer.finish();

        /* 本地上传oss */
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = Constant.OSS_END_POINT;
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = Constant.ACCESS_KEY;
        String accessKeySecret = Constant.SECRECT_KEY;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流。
        InputStream inputStream = new FileInputStream(fullName);
        ossClient.putObject(Constant.OSS_BUCKET_NAME, fullName, inputStream);
        /* 有效期十年 */
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        /* 预签名的相应Excel地址 */
        String url = ossClient.generatePresignedUrl(Constant.OSS_BUCKET_NAME, fullName, expiration).toString();
        /* 关闭OSSClient */
        ossClient.shutdown();

        /* 删除本地生成文件 */
        File file = new File(fullName);
        if (file.exists()) {
            file.delete();
        }

        /* 构建返回map */
        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("fileName", fullName);
        returnMap.put("fileUrl", url);

        return returnMap;
    }




}
